package codingDudes.display.basic;

import processing.core.PImage;
import codingDudes.display.DisplayObject;

/**
 * The DOImage class provides easy creation and display of images by
 * using the DisplayObject infrastructure.
 * 
 * @author Tobias Friese
 */

public class DOImage extends DisplayObject
{
	/** The PImage object holding pixel information of the loaded image. */
	protected PImage pImage;
	
	/**
	 * @param url The path to the source file.
	 */
	public DOImage(String url)
	{
		if(initialized) this.pImage = P5.loadImage(url);
		else throw new Error("The DisplayObject class has not yet been initialized. Call DisplayObject.initialize(pApplett) before using DisplayObjects.");
	}
	
	protected void draw()
	{
		this.pushMatrix();
		P5.image(this.pImage, 0, 0);
		this.popMatrix();
	}
	
	/* getter setter */
	
	/** Returns the generated PImage object. Might be null if image wasn't loaded correctly.*/
	public PImage getPImage()
	{
		return this.pImage;
	}
	
	/* finalization */
	
	/**
	 * Finalizes the DisplayObject instance. Call this if the instance is not
	 * needed anymore.
	 * 
	 * Will be called automatically before garbage collection.
	 */
	public void finalize()
	{
		super.finalize();
		
		this.pImage = null;
	}
}
